from models import *

# 增

# 增加课程到课程表
# course1 = Course(course_name="数据结构")
# db.session.add(course1)
# course2 = Course(course_name="Java语言")
# db.session.add(course2)
# course3 = Course(course_name="乐理")
# db.session.add(course3)
# course4 = Course(course_name="基音")
# db.session.add(course4)
# course5 = Course(course_name="物理化学")
# db.session.add(course5)
# course6 = Course(course_name="美声")
# db.session.add(course6)
# course7 = Course(course_name="公共演讲")
# db.session.add(course7)
#
# db.session.commit()


# 增加专业
# major1 = Major(id=2, major_name="地质学")
# db.session.add(major1)
#
# major2 = Major(id=1, major_name="软件工程")
# db.session.add(major2)
#
# major3 = Major(id=3, major_name="行政管理")
# db.session.add(major3)
#
# major4 = Major(id=4, major_name="音乐")
# db.session.add(major4)
#
# major5 = Major(id=5, major_name="英语")
# db.session.add(major5)
#
#
# db.session.commit()

# 增加学生
# stu1 = Student(name="乎日乐",major_id=2, gender="男", grade="大二")
# db.session.add(stu1)
# stu2 = Student(name="娜日太",major_id=1, gender="女", grade="大三")
# db.session.add(stu2)
# stu3 = Student(name="呼德日",major_id=5, gender="男", grade="大二")
# db.session.add(stu3)
# stu4 = Student(name="呼斯楞",major_id=1, gender="女", grade="研究生")
# db.session.add(stu4)
# stu5 = Student(name="图布",major_id=3, gender="男", grade="大二")
# db.session.add(stu5)
# stu6 = Student(name="乌日力嘎",major_id=2, gender="男", grade="大一")
# db.session.add(stu6)
# stu7 = Student(name="图亚",major_id=5, gender="女", grade="大四")
# db.session.add(stu7)
#
# db.session.commit()
# # 语句

# 添加成绩记录
# score1 = Score(student_id=1, course_id=1,term="上学期", score=68)
# db.session.add(score1)
# score2 = Score(student_id=1, course_id=2, term="下学期", score=89)
# db.session.add(score2)
# score3 = Score(student_id=5, course_id=7, term="上学期", score=99)
# db.session.add(score3)
# score4 = Score(student_id=3, course_id=7, term="上学期", score=87)
# db.session.add(score4)
# db.session.commit()

# 查
# 1. get 查
# stu1 = Student.query.get(1)
# stu2 = Student.query.get(2)
# print(stu1.name)
# print(stu2.name)

# 2. all()查询
# stu = Student.query.all()
# for s in stu:
#     print(s.name)

# 3. filter()查询-条件查询
# stu = Student.query.filter(Student.id >= 3)
# for s in stu:
#     print(s.name)
#
# stu = Student.query.filter(Student.name == "图布")
# for s in stu:
#     print(s.id, s.name)

# 4. filter_by()查询--- .all()   .first()    .filter()
# stu = Student.query.filter_by(name="乌日力嘎").first()
# print(stu.name, stu.id)
# 改
# stu = Student.query.filter(Student.id == 7)
# for s in stu.scores:
#     s.update({"score": 100})
# db.session.commit()


# 删
# stu = Student.query.filter(Student.name == "乌日力嘎").delete()
# print(stu)
# db.session.commit()


# majors = Major.query.get(2)
# for i in majors.students:
#     print(i.name, majors.major_name, i.id)


# 查询某个学生的成绩
# stu = Student.query.get(1)
# for i in stu.scores:
#     course = Course.query.get(i.course_id)
#     print(stu.name, course.course_name, i.score)

# 添加课程
# cs = Course.query.filter(Course.id > 2).all()
# print(cs)
#
# stu = Student.query.filter(Student.id == 4).first()
# stu.courses = cs
# db.session.add(stu)
# db.session.commit()


cs = Course.query.filter(Course.id > 5).all()

stu = Student.query.all()
for s in stu:
    s.courses = cs
    db.session.add(s)

db.session.commit()